<?php
/*
 * 凭证下载API
 * 密钥生成：密钥长度RSA2（2048）、密钥格式PKCS8；加密方法：RSA
 * 密钥可以在汇付宝后台生成，商户公钥保存在常规业务密钥里（可以参考新快捷密钥生成操作指南）
 * 凭证类型（1=分账,2=分账退回,3=银行卡充值,4=动账储值,5=线下汇款,6=提现,7=批付,8=批付批量获取）
 *
 */

//签名串排序

function ASCIIAZ($arrayData)
{

    ksort($arrayData);
    // 将数组转换为 URL 编码的字符串
    $data1 = http_build_query($arrayData);
    $data2 = urldecode($data1);
    return $data2;
}

/*
删除json指定键的值双引号
*/
function DeleteValue($jsonString)
{

    // 将 JSON 字符串解码为关联数组
    $arrayData = json_decode($jsonString, true);

// 处理 biz_content 键的值，去掉外层双引号
    if (isset($arrayData['biz_content'])) {
        $arrayData['biz_content'] = json_decode($arrayData['biz_content'], true);
    }

// 打印结果
    $arrayData1 = json_encode($arrayData);

    return $arrayData1;

}


//设置签名私钥
$PRIVATEkey =
    "-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC5XkR5gTRgWLAUeyCvnQZA4pdZ6ayV8R+//KzQjDW18SEaFb/g+9b4jX6tyWniEXWYhXrHXyTbI6ZvykRpD2HfMo1Bvb1OKIVZYKG9chJmcekGKufbmQ4Pxuoy72I8Qhi8zmO29pROE7nM7EHgh1aYtcpJIBmq9Zuc8BddIk6t4Cxo6NMtHSbgzXCcqw67zCRDyf8mLZIc+o5ObzNZqbqAwD7bRUctCbYlUumXNJYBzbQ3aNQxZow1GDw23zbqqPgRVI3iXyNX5FWipTMUgGDpo6wDF3hOT1Z0xRdtBfEkdT6UyruRKGbbWiDd9roa75W4mlmvM0y7g7ZhuVkoaJn7AgMBAAECggEAFGeKBg3w5AIfL8H18R7Jx26p1l3gf4jRQ+Leav5kEyj9yc9HFc7i9O0uWpbnLtlZJnba8wNIHU8pTPjo2t+0mEsKPYhTRD01oIcAhsf9uoUjb2hdqVCJubbdpM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RQLZoI0Qxvvn0nBjSh6/+iJD0=
-----END PRIVATE KEY-----";



//生成时间戳
$t=time();
$currentTime=date('Y-m-d H:i:s', $t);


//业务参数
$Oldbiz_content = array(
    "receipt_type" => '1',//凭证类型（1=分账,2=分账退回,3=银行卡充值,4=动账储值,5=线下汇款,6=提现,7=批付,8=批付批量获取）
    "trade_no" => 'H2311131893766AW',//汇元单号
    //"sub_merch_id" => "",//二级商户号（集团账号模式必传）

);
$Newbiz_content = json_encode($Oldbiz_content,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);//组装业务参数并转为JSON格式
echo $Newbiz_content .PHP_EOL;


//公共请求参数
$method = 'heepay.com.receipt.get';//具体业务接口名称
$version = '1.0';//版本号：固定值 1.0
$merch_id = '1664502';//商户编号，（汇付宝商户内码：七位整数数字）
$biz_content = $Newbiz_content;// Json参数集合，长度不限，具体见开发文档业务参数。
$timestamp = $currentTime;// 时间戳格式（注意有空格)yyyy-MM-dd HH：mm：ss

//生成签名串
$arrayData = array(
    'version' => $version,
    'biz_content' => $biz_content,
    'method' => $method,
    'merch_id' => $merch_id,
    'timestamp' => $timestamp,
);



$signatureContent = ASCIIAZ($arrayData);//组装参数
echo "签名串：" .$signatureContent .PHP_EOL;


//对签名串进行rsa签名
$private_key = openssl_pkey_get_private($PRIVATEkey);
openssl_sign($signatureContent, $signature, $private_key, OPENSSL_ALGO_SHA1);
$sign = base64_encode($signature);
//输出签名后数据
echo "签名结果：" .$sign . PHP_EOL;


// 构建请求的JSON数据
$request_data = array(
    'version' => $version,
    'biz_content' => $biz_content,
    'merch_id' => $merch_id,
    'method' => $method,
    'timestamp' => $timestamp,
    'sign' => $sign,
);
// 将数组转换为JSON字符串
$jsonString = json_encode($request_data,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
$jsonString1 = DeleteValue($jsonString);//删除json指定键的值双引号
//输出请求数据
echo "请求json：".$jsonString1 .PHP_EOL;


$Posturl = "https://pay.heepay.com/API/HywIndex.aspx";

// 创建Header选项
$options = array(
    'http' => array(
        'header'  => "Content-type: application/json\r\n",
        'method'  => 'POST',
        'content' => $jsonString1,
    ),
);

// 创建上下文选项
$context  = stream_context_create($options);
// 发送POST请求并获取响应

$response = file_get_contents($Posturl, false, $context);

if ($response === false) {
    // 处理请求失败的情况
    echo "Failed to send POST request.";
} else {
    // 处理请求成功的响应
    echo $response .PHP_EOL;
}
//解析响应数据
$Old_response = json_decode($response, true);
$New_response = $Old_response["data"];
$file_path = json_decode($New_response, true);
echo "下载凭证地址：" .$file_path["file_path"];


